<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>SSR自动发布系统</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      body {
        font-family: 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif;
      }
      .wrap {
        text-align: center;
        display: flex;
        justify-content: space-between;
      }
      .left-box,
      .right-box {
        padding: 20px;
        width: 50%;
      }
      .right-box {
        border-left: 1px solid #e6e6e6;
      }
      .el-input__inner {
        -webkit-appearance: none;
        background-color: #fff;
        background-image: none;
        border-radius: 4px;
        border: 1px solid #dcdfe6;
        box-sizing: border-box;
        color: #606266;
        display: inline-block;
        font-size: inherit;
        height: 40px;
        line-height: 40px;
        outline: none;
        padding: 0 15px;
        transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
        width: 100%;
      }

      .el-input__inner::placeholder {
        color: #c0c4cc;
      }

      .el-input__inner:hover {
        border-color: #c0c4cc;
      }

      .el-input__inner:focus {
        outline: none;
        border-color: #409eff;
      }

      .el-input__suffix {
        position: absolute;
        height: 100%;
        right: 5px;
        top: 0;
        text-align: center;
        color: #c0c4cc;
        transition: all 0.3s;
        pointer-events: none;
      }
      .el-button {
        display: block;
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        background: #fff;
        border: 1px solid #dcdfe6;
        color: #606266;
        -webkit-appearance: none;
        text-align: center;
        box-sizing: border-box;
        outline: none;
        margin: 0;
        transition: 0.1s;
        font-weight: 500;
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 4px;
        width: 100%;
      }

      @media screen and (max-width: 1000px) {
        .left-box,
        .right-box {
          width: 100%;
          box-sizing: border-box;
        }
        .wrap {
          display: block;
        }
      }

      .el-button + .el-button {
        margin-left: 10px;
      }

      .el-button.is-round {
        padding: 12px 20px;
      }

      .el-button:focus,
      .el-button:hover {
        color: #409eff;
        border-color: #c6e2ff;
        background-color: #ecf5ff;
      }

      .el-button:active {
        color: #3a8ee6;
        border-color: #3a8ee6;
        outline: none;
      }

      .el-button::-moz-focus-inner {
        border: 0;
      }

      .el-button [class*='el-icon-'] + span {
        margin-left: 5px;
      }

      .el-button.is-plain:focus,
      .el-button.is-plain:hover {
        background: #fff;
        border-color: #409eff;
        color: #409eff;
      }

      .el-button.is-plain:active {
        background: #fff;
        outline: none;
      }

      .el-button--primary {
        color: #fff;
        background-color: #409eff;
        border-color: #409eff;
      }

      .el-button--primary:focus,
      .el-button--primary:hover {
        background: #66b1ff;
        border-color: #66b1ff;
        color: #fff;
      }

      .el-button--primary:active {
        outline: none;
      }

      .el-button--primary.is-active,
      .el-button--primary:active {
        background: #3a8ee6;
        border-color: #3a8ee6;
        color: #fff;
      }

      .el-button--primary.is-disabled,
      .el-button--primary.is-disabled:active,
      .el-button--primary.is-disabled:focus,
      .el-button--primary.is-disabled:hover {
        color: #fff;
        background-color: #a0cfff;
        border-color: #a0cfff;
      }

      #info-box {
        text-align: left;
        font-size: 12px;
        background-color: #333;
        color: #fff;
        text-align: center;
        padding: 20px;
        line-height: 1.6;
        margin: 0 auto;
        margin-top: 20px;
        min-height: 280px;
      }
      #button {
        margin-top: 20px;
      }
      .notice {
        font-size: 14px;
        color: #f00;
        margin-top: 20px;
        font-weight: bold;
      }
      .record-box {
        line-height: 1.6;
      }
    </style>
  </head>
  <body>
    <div class="wrap">
      <div class="left-box">
        <input type="password" id="password" placeholder="请输入口令" class="el-input__inner" />
        <br />
        <button type="button" id="button" class="el-button el-button--primary is-round"><!----><!----><span>开始部署新服务器</span></button>
        <div id="info-box"></div>
      </div>
      <div class="right-box">
        <p>历史发布记录：</p>
        <pre class="record-box"></pre>
        <p class="notice">注意：最后一条IP为最新IP，请确定此IP不能ping通之后再部署新的IP。</p>
      </div>
    </div>

    <script>
      let ip = location.hostname;
      let wsClientId = 'protocol' + new Date().getTime() + Math.round(Math.random() * 100000);
      function createSocket() {
        //创建一个socket连接
        let socket = new WebSocket(`ws://${ip}:1002`, wsClientId);
        socket.onmessage = function (e) {
          appendLog(e.data);
        };
      }
      createSocket();

      //将输出追加到屏幕上
      let logBox = document.querySelector('#info-box');
      function appendLog(content) {
        let p = document.createElement('p');
        p.innerText = content;
        logBox.append(p);
      }

      //点击开始发布
      document.querySelector('#button').addEventListener('click', function () {
        fetchData(`http://${ip}:1011/deploy`, {
          password: document.querySelector('#password').value,
          clientId: wsClientId,
        }).then((res) => {
          if (res.msg === '口令错误') {
            alert('口令错误，请重试');
          }
        });
      });

      //关闭的时候关掉websocket
      window.onunload = function () {
        socket.close();
      };

      //查询之前的发布记录
      function queryDeployHistory() {
        fetchData(`http://${ip}:1011/deploy/record`, {}).then((res) => {
          document.querySelector('.record-box').innerHTML = res.content;
        });
      }
      queryDeployHistory();

      //fetch封装
      function fetchData(url, data, method = 'POST') {
        return fetch(url, {
          body: JSON.stringify(data),
          method,
          headers: {
            'content-type': 'application/json',
          },
          mode: 'cors',
        }).then((response) => response.json()); // parses response to JSON
      }
    </script>
  </body>
</html>
